package com.nyist.red;

import java.util.concurrent.CountDownLatch;

public class UserThread implements Runnable {
    private final RedEnvelope redEnvelope;
    //用来存放一个闭锁
    private final CountDownLatch latch;

    public UserThread(RedEnvelope redEnvelope, CountDownLatch latch) {
        this.redEnvelope = redEnvelope;
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            double money = redEnvelope.getRandomMoney();
            if (money <= 0) {
                System.out.println(Thread.currentThread().getName() + "不好意思，您手慢了！");
            } else {
                // System.out.println(Thread.currentThread().getName() + "抢到 " + money + "元");
                System.out.println(money);
            }
        } finally {
            latch.countDown();//让CountDownLatch计数器减1
        }
    }
}